package es.ats.codington.festival.services;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.services.iViewAllEvents;
import es.ats.codington.festival.interfaces.services.iViewEvent;
import es.ats.codington.festival.interfaces.services.iViewMyEvents;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;

/**
 * 
 * @author CODINGTONPOJO
 *
 */
	
public class EventService implements  iViewEvent, iViewAllEvents, iViewMyEvents {

	private Connection connection;
	
	/** Constructor
	 * 
	 * @param connection
	 */

	public EventService(Connection connection) {
		super();
		this.connection = connection;
	}

	
	public EventService() {
		super();
	}
	
	
	@Override
	public Event viewEvent(int idevent) throws Exception {
		Event event = null;
		
		// TODO Auto-generated method stub
		
		try{
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("It does not connect to DataBase");
			EventDAOImpl eventdaoimpl=new EventDAOImpl(connection);
			event=eventdaoimpl.viewEvent(idevent);
			
			}catch (Exception ex){
				System.out.println("Error Exception with message: " + ex.getMessage());
				connection.rollback();
			}
			connection.close();
			
		return event;
	}
		
	@Override
	public ArrayList<Event> viewAllEvents() throws Exception {
		ArrayList<Event> arraylist = null;
		
		// TODO Auto-generated method stub
		
		try{
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("It does not connect to DataBase");
			
			EventDAOImpl eventdaoimpl=new EventDAOImpl(connection);
			arraylist=eventdaoimpl.viewAllEvents();
			
			}catch (Exception ex){
				System.out.println("Error Exception with message: " + ex.getMessage());
				connection.rollback();
			}
			connection.close();
			
		return arraylist;
	}
	
	@Override
	public ArrayList<Integer> viewMyEvent(int idvisitor) throws Exception {
		
		ArrayList<Integer> idMyEvents = new ArrayList<Integer>();
		try{
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("It does not connect to DataBase");
			
			EventDAOImpl eventdaoimpl=new EventDAOImpl(connection);
			idMyEvents = eventdaoimpl.viewMyEvents(idvisitor);
			
			}catch (Exception ex){
				System.out.println("Error Exception with message: " + ex.getMessage());
				connection.rollback();
			}
			connection.close();
			
		return idMyEvents;
		
	}
	
public ArrayList<Integer> getIdsEvent(int idplace) throws Exception {
		
	ArrayList<Integer> idsEventByType= new ArrayList<Integer>();
		try{
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("It does not connect to DataBase");
			
			EventDAOImpl eventdaoimpl=new EventDAOImpl(connection);
			idsEventByType = eventdaoimpl.getIdsEvent(idplace);
			
			}catch (Exception ex){
				System.out.println("Error Exception with message: " + ex.getMessage());
				connection.rollback();
			}
			connection.close();
			
		return idsEventByType;
		
	}

}


